package com.gnerv.cms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.cms.dto.StatisticsApiDTO;
import com.gnerv.cms.dto.StatisticsApiSearchDTO;
import com.gnerv.cms.entity.StatisticsApi;
import com.gnerv.cms.mapper.StatisticsApiMapper;
import com.gnerv.cms.service.IStatisticsApiService;
import com.gnerv.cms.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Future;

/**
 * <p>
 * 基础平台-权限表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Slf4j
@Service
public class StatisticsApiServiceImpl extends ServiceImpl<StatisticsApiMapper, StatisticsApi> implements IStatisticsApiService {

    @Resource
    private StatisticsApiMapper statisticsApiMapper;

    @Async(value = "StatisticsTaskExecutor")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Future<String> insertStatisticsApi(StatisticsApi statisticsApi) {
        statisticsApi.setUkId(UUIDUtils.getLower());
        statisticsApiMapper.insertStatisticsApi(statisticsApi);
        return new AsyncResult<>("true");
    }

    @Override
    public Future<String> insertOrUpdateStatisticsApi(StatisticsApi statisticsApi) {
        statisticsApi.setUkId(UUIDUtils.getLower());
        statisticsApiMapper.insertOrUpdateStatisticsApi(statisticsApi);
        return new AsyncResult<>("true");
    }

    @Async(value = "StatisticsTaskExecutor")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Future<String> updatePathTotal(String path) {
        statisticsApiMapper.updatePathTotal(path);
        return new AsyncResult<>("true");
    }

    @Override
    @Transactional(readOnly = true)
    public List<StatisticsApiDTO> listStatisticsApiDTO(StatisticsApiSearchDTO search) {
        List<StatisticsApiDTO> statisticsApiDTOS = statisticsApiMapper.listStatisticsApiDTO(search);
        return statisticsApiDTOS;
    }

}
